using System;
using System.Collections.Generic;

using IBatisNet.DataMapper;

using log4net;

using GymManager.Exception;
using GymManager.Persistence.VO;

namespace GymManager.Persistence.DAO
{
    /// <summary>
    /// Class responsible by getting information from database about medical exam
    /// </summary>
    public class MedicalExamDAO
    {
        // logger instance
        private ILog _logManager = LogManager.GetLogger(typeof(MedicalExamDAO));

        /// <summary>
        /// Method responsible by getting all medical exams beloging to an specific student
        /// </summary>
        /// <param name="Int32">student identifier</param>
        /// <returns>IList - list of medical exams belonging to an specific student</returns>
        /// <see cref="GymManager.Exception.Persistence"/>
        public IList<MedicalExam> findByStudent(Int32 studentId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // user value object to be returned
            IList<MedicalExam> medicalExamList = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                medicalExamList = ibatisMapper.QueryForList<MedicalExam>("MedicalExam.findByStudent", studentId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving medical exam information", ex);
                throw new PersistenceException("Error while retrieving medical exam information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return medicalExamList;
        }

        /// <summary>
        /// Method responsible by getting the most recent medical exam beloging to an specific student
        /// </summary>
        /// <param name="Int32">student identifier</param>
        /// <returns>Medical Exam - most recent medical exam belonging to an specific student</returns>
        /// <see cref="GymManager.Exception.Persistence"/>
        public MedicalExam findMostRecentByStudent(Int32 studentId)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            // most recent medical exam
            MedicalExam mostRecentMedicalExam = null;

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                mostRecentMedicalExam = ibatisMapper.QueryForObject<MedicalExam>("MedicalExam.findMostRecentByStudent", studentId);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while retrieving medical exam information", ex);
                throw new PersistenceException("Error while retrieving medical exam information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }

            return mostRecentMedicalExam;
        }

        /// <summary>
        /// Method responsible by adding a new medical exam to database
        /// </summary>
        /// <param name="MedicalExam">medical exam information to be saved</param>
        /// <see cref="GymManager.Exception.PersistenceException"/>
        public void create(MedicalExam medicalExamVO)
        {
            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("Begin Method");
            }

            try
            {
                ISqlMapper ibatisMapper = DAOManager.getInstance();
                ibatisMapper.Insert("MedicalExam.insert", medicalExamVO);
            }
            catch (System.Exception ex)
            {
                _logManager.Error("Error while saving medical exam information", ex);
                throw new PersistenceException("Error while saving medical exam information", ex);
            }

            if (_logManager.IsInfoEnabled)
            {
                _logManager.Info("End Method");
            }
        }
    }
}
